package main

import (
	"context"
	"git.900sui.cn/kc/base/utils"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/bus"
	busEs "git.900sui.cn/kc/rpcinterface/client/elastic/bus"
	bus2 "git.900sui.cn/kc/rpcinterface/interface/bus"
	"github.com/streadway/amqp"
	"strconv"
)
//@author lixiaojun
//消费完成后 店铺会员消费金额，消费次数，最后消费时间数据统计
func main()  {
	rpcBusMember := new(bus.Member).Init()
	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	new(utils.KcLog).GetInstance(kcgin.AppConfig.String("queue.log.consumeSucMemberCount"))
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.consumeSucMemberCount"), func(msg amqp.Delivery) {
		consumeLogId, _ := strconv.Atoi( string(msg.Body) )
		var reply bus2.ReplyAddMemberConsumeCount
		err := rpcBusMember.AddMemberConsumeCount(context.Background(), &consumeLogId, &reply)
		if err != nil {
			utils.Logerr.Printf("消费成功，店铺会员消费数据统计任务执行失败，orderSn:%d, err:%s", consumeLogId, err)
		}
		utils.Logerr.Printf("消费成功，店铺会员消费数据统计任务执行成功，orderSn:%d", consumeLogId)
		memberId:=reply.MemberId
		if memberId>0{
			//将数据同步更新到es
			rpcBusMemberEs:=new(busEs.Member).Init()
			defer rpcBusMemberEs.Close()
			var rpcBusMemberEsRes bool
			if err:=rpcBusMemberEs.SetMemberConsumeCount(context.Background(),&memberId,&rpcBusMemberEsRes);err!=nil{
				utils.Logerr.Printf("店铺会员消费数据统计更新到ES 任务执行失败，orderSn:%d ,memberId:%d, err:%s", consumeLogId,memberId, err)
			}
			utils.Logerr.Printf("店铺会员消费数据统计更新到ES 任务执行成功，orderSn:%d ,memberId:%d", consumeLogId,memberId)
		}
	})

	if err != nil {
		utils.Logerr.Printf("消费成功后的店铺会员消费数据统计任务执行错误, err: %s", err.Error())
	}
}